package com.match.competition_sub.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.match.competition_sub.entity.Course;
import com.match.competition_sub.entity.vo.CourseQuery;
import com.match.competition_sub.excel.CourseData;
import com.match.competition_sub.listener.CourseExcelListener;
import com.match.competition_sub.mapper.CourseMapper;
import com.match.competition_sub.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-21
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    /**
     * 通过Excel导入课程信息
     * @param file
     * @param courseService
     */
    @Override
    public void addCourseByExcel(MultipartFile file, CourseService courseService) {
        try{
            //文件输入流
            InputStream in=file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, CourseData.class,new CourseExcelListener(courseService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 分页条件查询课程信息
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @Override
    public Map<String, Object> findCourseByQueryPage(long current, long limit, CourseQuery courseQuery) {
        //创建Page对象
        Page<Course> coursePage=new Page<>(current,limit);
        //构建条件查询封装对象
        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
        //封装查询条件
        String courseName = courseQuery.getCourseName();
        String courseType = courseQuery.getCourseType();
        String department = courseQuery.getDepartment();
        String majorName = courseQuery.getMajorName();
        String grade = courseQuery.getGrade();
        if (!StringUtils.isEmpty(courseName)){
            courseQueryWrapper.like("course_name",courseName);
        }
        if (!StringUtils.isEmpty(courseType)){
            courseQueryWrapper.like("course_type",courseType);
        }
        if (!StringUtils.isEmpty(department)){
            courseQueryWrapper.like("department",department);
        }
        if (!StringUtils.isEmpty(majorName)){
            courseQueryWrapper.like("major_name",majorName);
        }
        if (!StringUtils.isEmpty(grade)){
            courseQueryWrapper.eq("grade",grade);
        }
        courseQueryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(coursePage,courseQueryWrapper);
        long total = coursePage.getTotal();
        List<Course> records = coursePage.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return map;
    }
}
